<?php
$csvrows = array();
$csvrows[] = array("id","ip_addr","rndnum","rndstr");
$aZ09 = array_merge(range('A', 'Z'), range('a', 'z'),range(0, 9)); 
for($i = 0; $i < 999999; $i++)
{
	$ip_addr = (string)mt_rand(1,255) . "." . (string)mt_rand(1,255) . "." . (string)mt_rand(1,255) . "." . (string)mt_rand(1,255);
	$randnum = mt_rand(0,9999);
	$randstr = "";
	for ($x = 0; $x < 10; $x++)
		$randstr .= $aZ09[rand(0,61)];
	$csvrows[] = array((string)$i, $ip_addr, $randnum, $randstr);
}

$fp = fopen('/tmp/csv.tmp', 'w');

foreach($csvrows as $row)
	fputcsv($fp, $row);

$csvrows = array();	
fclose($fp);

$fp2 = fopen("/tmp/csv.tmp", 'r');
$row = fgetcsv($fp2); //Skip first header row
$highestaddr = 0;
$curraddr = 0;
while(($row = fgetcsv($fp2)) !== false)
{
	if ($row)
	{
		$ip_addr_arr = explode(".", $row[1]);
		$oct1 = str_pad(decbin(intval($ip_addr_arr[0])), 8, "0", STR_PAD_LEFT);
		$oct2 = str_pad(decbin(intval($ip_addr_arr[1])), 8, "0", STR_PAD_LEFT);
		$oct3 = str_pad(decbin(intval($ip_addr_arr[2])), 8, "0", STR_PAD_LEFT);
		$oct4 = str_pad(decbin(intval($ip_addr_arr[3])), 8, "0", STR_PAD_LEFT);
		$curraddr = intval($oct1.$oct2.$oct3.$oct4, 2);
		if ($highestaddr < $curraddr)
			$highestaddr = $curraddr;
	}
}

fclose($fp2);

$octhigh1 = 0;
$octhigh2 = 0;
$octhigh3 = 0;
$octhigh4 = 0;
echo "Highest IP Address generated (Decimal): " . $highestaddr . "\n";
$octhigh1 = $highestaddr & 0xFF;
$octhigh2 = ( $highestaddr >> 8 ) & 0xFF;
$octhigh3 = ( $highestaddr >> 16 ) & 0xFF;
$octhigh4 = ( $highestaddr >> 24 ) & 0xFF;
echo "Highest IP Address generated (Dotted): " . (string)$octhigh4 . "." . (string)$octhigh3 . "." . (string)$octhigh2 . "." . (string)$octhigh1 . "\n";
unlink("/tmp/csv.tmp");
?>